<?php
/**
 * @title 36. 有效的数独 - 哈希
 * @author start2004
 */

class Solution {

    /**
     * @param String[][] $board
     * @return Boolean
     */
    function isValidSudoku($board) {
        /**
         * @since 2020-07-15 哈希数组
         */
        $hashs = [];

        /**
         * @since 2020-07-15 3x3区域编号
         */
        $areaNum = -3;

        /**
         * @since 2020-07-15 开始迭代
         */
        for ($h=0; $h<=8; $h++){
            /**
             * @since 2020-07-15 区域变化
             */
            if($h%3 == 0){
                $areaNum += 3;
            } else {}

            /**
             * @since 2020-07-15 区域增量
             */
            $areaAdd = -1;
            for($w=0; $w<=8; $w++){
                /**
                 * @since 2020-07-15 区域变化
                 */
                if($w%3 == 0){
                    $areaAdd++;
                } else {}

                /**
                 * @since 2020-07-15 数值
                 */
                $num = $board[$h][$w];
                if($num != "."){
                    /**
                     * @since 2020-07-15 行是否存在
                     */
                    if(isset($hashs[$num]["row"][$h])){
                        // var_dump($hashs[$num]);
                        // echo "num={$num}, h={$h}, w={$w}, area={$area}", PHP_EOL;
                        return false;
                        break;
                    } else {
                        $hashs[$num]["row"][$h] = 1;
                    }

                    /**
                     * @since 2020-07-15 列是否存在
                     */
                    if(isset($hashs[$num]["column"][$w])){
                        // var_dump($hashs[$num]);
                        // echo "num={$num}, h={$h}, w={$w}, area={$area}", PHP_EOL;
                        return false;
                        break;
                    } else {
                        $hashs[$num]["column"][$w] = 1;
                    }

                    /**
                     * @since 2020-07-15 当前3x3区域
                     */
                    $area = $areaNum+$areaAdd;

                    /**
                     * @since 2020-07-15 区域是否存在
                     */
                    if(isset($hashs[$num]["area"][$area])){
                        // var_dump($hashs[$num]);
                        // echo "num={$num}, h={$h}, w={$w}, area={$area}", PHP_EOL;
                        return false;
                        break;
                    } else {
                        $hashs[$num]["area"][$area] = 1;
                    }
                } else {}
            }
        }

        /**
         * @return
         */
        return true;
    }
}




/**
 * @url http://127.0.0.1/leetcode/202007/2020.07.15_4.php
 */
$datas = [
    [
        ["8","3",".",  ".","7",".",  ".",".",".",],
        ["6",".",".",  "1","9","5",  ".",".",".",],
        [".","9","8",  ".",".",".",  ".","6",".",],

        ["8",".",".",  ".","6",".",  ".",".","3",],
        ["4",".",".",  "8",".","3",  ".",".","1",],
        ["7",".",".",  ".","2",".",  ".",".","6",],

        [".","6",".",  ".",".",".",  "2","8",".",],
        [".",".",".",  "4","1","9",  ".",".","5",],
        [".",".",".",  ".","8",".",  ".","7","9",],
    ],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->isValidSudoku($data);
    var_dump($result);
    echo str_repeat("<br>", 3);
}
$xhprof->end();
